package ro.msg.quiz.validator;

import ro.msg.quiz.domain.Question;
import ro.msg.quiz.domain.SingleChoiceQuestion;
import ro.msg.quiz.exception.MyException;

//AUTHORS: Elod, Marco

//Class that validates a SingleChoiceQuestion
public class SingleChoiceQuestionValidator extends QuestionValidator {

    @Override
    public void validate(Question o) throws MyException {

	StringBuilder sb = new StringBuilder();
	if (o instanceof SingleChoiceQuestion) {
	    SingleChoiceQuestion q = (SingleChoiceQuestion) o;
	    if (q.getOptions().size() < 1) {
		sb.append("The question doesn't have any possible answers!\n");
	    }
	    if (q.getCorrectIndex() < 0 || q.getCorrectIndex() > q.getOptions().size()) {
		sb.append("The index of the correct answer must be between 0 and the number of possible answers!\n");
	    }
	} else
	    sb.append("The object is not an instance of SingleChoiceQuestion!\n");

	if (sb.length() > 0) {
	    throw new MyException(sb.toString());
	}

    }

}
